<?php

namespace App\Console\Commands;

use App\Customer;
use App\Lead;
use App\Record;
use Illuminate\Console\Command;

class ProjectChange extends Command
{
    /**
     * The name and signature of the console command.
     *
     * @var string
     */
    protected $signature = 'project:change';

    /**
     * The console command description.
     *
     * @var string
     */
    protected $description = 'Command description';

    /**
     * Create a new command instance.
     *
     * @return void
     */
    public function __construct()
    {
        parent::__construct();
    }

    /**
     * Execute the console command.
     *
     * @return mixed
     */
    public function handle()
    {

        Lead::where('leads_id','>=',46789)
            ->where('is_send',0)
            ->chunkById(10, function ($leads) {
                foreach ($leads as $lead) {

                    $sudoLeads = Lead::where('mobile', $lead->mobile)->where(
                        'leads_id', '!=', $lead->leads_id
                    )->get();

                    if ($sudoLeads->isNotEmpty()) {
                        foreach ($sudoLeads as $sudoLead) {
                            if ($sudoLead->customer_id != 0) {
                                $records = Record::where('types', 'crm_customer')
                                    ->where('types_id', $sudoLead->customer_id)
                                    ->get();

                                $this->setRecord($records,$sudoLead,$lead);
                                $this->setRecordCustomer($records,$sudoLead,$lead);
                            }

                            $records = Record::where('types', 'crm_leads')
                                ->where('types_id', $sudoLead->leads_id)
                                ->get();
                            $this->setRecord($records,$sudoLead,$lead);
                            $this->setRecordCustomer($records,$sudoLead,$lead);
                        }
                    }
                    $lead->is_send=1;
                    $this->info($lead->leads_id);
                    $lead->save();
                }
            });
    }

    public function  setRecord($records,$sudoLead,$lead){
        if ($records->isNotEmpty()) {
            foreach ($records as $record) {
                $record2 = Record::where('types_id', $lead->leads_id)
                    ->where('types','crm_leads')
                    ->where('create_time', $record->create_time)
                    ->first();

                if ($record2) {
                    $this->info('存在');
                    continue;
                }
                $record1 = new Record();
                $record1->types = 'crm_leads';
                $record1->types_id = $lead->leads_id;
                $record1->content = $record->content;
                $record1->category = $record->category;
                $record1->next_time = $record->next_time;
                $record1->business_ids = $record->business_ids;
                $record1->create_time = $record->create_time;
                $record1->update_time = $record->update_time;
                $record1->create_user_id = $record->create_user_id;
                $record1->status = $record->status;
                $record1->project_id = $record->project_id;
                $record1->save();
                $this->info($record1->record_id);

            }
        }
    }
    public function  setRecordCustomer($records,$sudoLead,$lead){
        if ($lead->customer_id!=0 ) {
            if ($records->isNotEmpty()) {
                foreach ($records as $record) {
                    $record2 = Record::where('types_id', $lead->customer_id)
                        ->where('create_time', $record->create_time)
                        ->first();
                    if ($record2) {
                        continue;
                    }
                    $record1 = new Record();
                    $record1->types = 'crm_customer';
                    $record1->types_id = $lead->customer_id;
                    $record1->content = $record->content;
                    $record1->category = $record->category;
                    $record1->next_time = $record->next_time;
                    $record1->business_ids = $record->business_ids;
                    $record1->create_time = $record->create_time;
                    $record1->update_time = $record->update_time;
                    $record1->create_user_id = $record->create_user_id;
                    $record1->status = $record->status;
                    $record1->project_id = $record->project_id;
                    $record1->save();
                    $this->info($record1->record_id . 'customer');
                }
            }
        }
    }

}
